package com.xyrl.common.core.config;

import com.xyrl.common.core.properties.SwaggerProperties;
import com.xyrl.common.core.properties.XyrlProperties;
import com.xyrl.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

/**
 * @author wx
 */
@Configuration
//@EnableSwagger2
public class XyrlConfig {
    @Autowired
    private XyrlProperties properties;


//    @Bean
//    public Docket swaggerApi() {
//        SwaggerProperties swagger = properties.getSwagger();
//        return new Docket(DocumentationType.SWAGGER_2)
//                .enable(true)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage()))
//                .paths(PathSelectors.any())
//                .build()
//                .apiInfo(apiInfo(swagger));
//    }

    /**
     * 缓存管理
     *
     * @return
     */
    @Bean("xyrlRedisUtil")
    public RedisUtil cacheManager(@Qualifier("cacheRedisTemplate") RedisTemplate<String, Object> cacheRedisTemplate) {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.initRedisTemplate(cacheRedisTemplate);
        return redisUtil;
    }


    /**
     * 自定义缓存序列化方式
     *
     * @param factory
     * @return
     */
    @Bean(name = "cacheRedisTemplate")
    public RedisTemplate<String, Object> cacheRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

//    private ApiInfo apiInfo(SwaggerProperties swagger) {
//        return new ApiInfo(
//                swagger.getTitle(),
//                swagger.getDescription(),
//                swagger.getVersion(),
//                null,
//                new Contact(swagger.getAuthor(), swagger.getUrl(), swagger.getEmail()),
//                swagger.getLicense(), swagger.getLicenseUrl(), Collections.emptyList());
//    }

    /**
     * 从springboot 2.2.0.M3开始，HttpTraceRepository将变成条件式声明，不再通过自动配置声明。就是默认禁用了management.trace.http
     *
     * @return
     */
    @ConditionalOnMissingBean
    @Bean
    public HttpTraceRepository httpTraceRepository() {
        return new InMemoryHttpTraceRepository();
    }
}
